package cutetd.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import cutetd.dao.UserDao;
import cutetd.model.M_User;
import cutetd.vo.Page;
import cutetd.vo.PageNavi;

public class UserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<M_User> userList = new ArrayList<M_User>();
	private M_User user;
	private UserDao userDao = new UserDao();
	private String email;

	// 控制分页属性
	private int curPage = 1;
	private List<Page> pageList = new ArrayList<Page>();
	private int totalPage;

	private PageNavi pageNavi = new PageNavi(M_User.class, 2, 5);

	public String managerLogin() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.setAttribute("email", email);
		session.setAttribute("type", "admin");
		return "login_success";
		/*if (user != null) {
			M_User tmpUser = userDao.getObjectByKey(user.getEmail());
			if (tmpUser != null && tmpUser.getPwd().equals(user.getPwd())) {
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpSession session = request.getSession();
				session.setAttribute("email", tmpUser.getEmail());
				session.setAttribute("type", tmpUser.getType());
				return "login_success";
			}
		}
		return INPUT;*/
	}

	public String cxpinLogin() {
		
		M_User tmpUser = userDao.getObjectByKey(user.getEmail());
		if (tmpUser.getPwd().equals(user.getEmail())) {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			session.setAttribute("email", tmpUser.getEmail());
			session.setAttribute("type", tmpUser.getType());
		}

		return "login";
	}

	public String register() {
		return "register";
	}

	public String addUser() {
		user.setDate(new Date());
		userDao.saveOrUpdate(user);
		return "toAdd";
	}

	public String toAdd() {
		return "toAdd";
	}

	@SuppressWarnings("unchecked")
	public String manager() {
		pageList = pageNavi.getPageList(curPage);
		setUserList((List<M_User>) pageNavi.getObjectList(curPage,
				"date != '1'", "date asc"));
		setTotalPage(pageNavi.getTotalPages());
		return SUCCESS;
	}

	public String toUpdate() {
		user = userDao.getObjectByKey(email);
		return "toUp";
	}

	public String updateUser() {
		userDao.saveOrUpdate(user);
		return "manager";
	}

	public String deleteUser() {
		userDao.delete(email);
		return "manager";
	}

	public List<M_User> getUserList() {
		return userList;
	}

	public void setUserList(List<M_User> userList) {
		this.userList = userList;
	}

	public List<Page> getPageList() {
		return pageList;
	}

	public void setPageList(List<Page> pageList) {
		this.pageList = pageList;
	}

	public M_User getUser() {
		return user;
	}

	public void setUser(M_User user) {
		this.user = user;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
}
